<?php
/**
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 16/8/4
 * Time: 下午2:31
 */

namespace Backend\Controller;


use Org\Util\Tree;

class AreaController extends BaseController
{

    private $mod;
    public function __construct()
    {
        parent::__construct();
        $this->mod = D('area');
        ini_set('memory_limit','-1');
    }

    public function index()
    {

        if(!S('area_tree'))
        {
            $tree = new Tree();
            $tree->icon = array('│ ','├─ ','└─ ');
            $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
            $result = $this->mod->where('level <= 3')->order('ordid asc')->select();

            $array = array();
            foreach($result as $r) {
                $r['status'] = ($r['status'] ==1) ? '<span style=" color: #00CC00;" data-field="status" class="glyphicon glyphicon-ok"></span>' : '<span style=" color: red;" data-field="status" class="glyphicon glyphicon-remove"></span>';
                $r['str_status'] = '<img data-tdtype="toggle" data-id="'.$r['id'].'" data-field="status" data-value="'.$r['status'].'" src="__STATIC__/images/admin/toggle_' . ($r['status'] == 0 ? 'disabled' : 'enabled') . '.gif" />';
                $r['str_manage'] = '<a href="'.U('backend/area/add',array('pid'=>$r['id'])).'">添加子地区</a> |
                                <a href="'.U('backend/area/edit',array('id'=>$r['id'])).'">'.L('edit').'</a> |
                                <a href="javascript:;" class="layer_confirm" data-url="'.U('backend/area/delete',array('id'=>$r['id'])).'" data-msg="'.sprintf(L('confirm_delete_one'),$r['cate_name']).'">'.L('delete').'</a>';
                $r['parentid_node'] = ($r['pid'])? ' class="child-of-node-'.$r['pid'].'"' : '';
                $array[] = $r;
            }
            $str  = "<tr id='node-\$id' \$parentid_node>
                <td align='left'><input type='checkbox' value='\$id' class='J_checkitem'></td>
                <td>\$spacer<span data-tdtype='edit' data-field='name' data-id='\$id' class='tdedit'>\$name</span></td>
                <td align='left'>\$id</td>
                <td align='left'><span data-tdtype='edit' data-field='ordid' data-id='\$id' class='tdedit'>\$ordid</span></td>

                <td align='left'>\$status</td>
                <td align='left'>\$str_manage</td>
                </tr>";
            $tree->init($array);

            $list = $tree->get_tree(0, $str);
            S('area_tree', $list);
        } else {
            $list = S('area_tree');
        }
        $this->assign('list', $list);
        $big_menu = array(
            'title' => '新地区',
            'uri' => U('area/add'),
        );
        $this->assign('big_menu', $big_menu);
        $this->assign('list_table', true);
        $this->display();
    }


    public function edit()
    {
        $id   = I('id');
        if(IS_AJAX)
        {
            $this->mod->create();
            $this->mod->save();
            $back = array(
                'status' => 1,
                'msg'    => L('operation_success'),
            );
            $this->ajaxReturn($back);
        }
        $area = $this->mod->find($id);
        $tree = new Tree();
        $result = $this->mod->where('level <= 3')->order('ordid asc')->select();
        $array  = array();
        foreach($result as $r) {
            $r['selected'] = $r['id'] == $area['pid'] ? 'selected' : '';
            $array[] = $r;
        }
        $str  = "<option class='_area' value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($array);
        $select_menus = $tree->get_tree(0, $str);
        $this->assign('select_menus', $select_menus);
        $this->assign('area', $area);
        $this->display();
    }


    public function add()
    {
        if(IS_AJAX)
        {
            if($this->mod->create())
            {
                $this->mod->add();
                $back = array(
                    'status' => 1,
                    'msg'    => L('operation_success'),
                );
                $this->ajaxReturn($back);
            } else {
                $msg = $this->mod->getError();
                $back = array(
                    'status' => 1,
                    'msg'    => $msg,
                );
                $this->ajaxReturn($back);
            }

        }


        //地区父级
        $area = $this->mod->getChildren(0, $order = 'ordid asc');
        $this->assign('area', $area);
        $this->display();
    }


    public function children()
    {
        if(IS_AJAX)
        {
            $pid  = I('id');
            $data = $this->mod->getChildren($pid, $order = 'ordid asc');
            if($data) $this->ajaxReturn(array('status' => 1, 'data' => $data));
            $this->ajaxReturn(array('status' => 0, 'data' => ''));
        }
    }



}